package com.baimifan.renovation.adapter;

import android.graphics.Color;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.baimifan.renovation.R;
import com.baimifan.renovation.model.OneClassifyData;
import com.baimifan.renovation.view.OneClassifyHolder;

import java.util.ArrayList;

/**
 * Created by baimifan on 2017/07/04.
 */

public class OneClassifyAdapter extends BaseAdapter {
    private ArrayList<OneClassifyData> mlist;
    private Float w, h;
    private int selectedPosition = -1;

    public OneClassifyAdapter (ArrayList<OneClassifyData> list, Float w, Float h) {
        this.mlist = list;
        this.w = w;
        this.h = h;
    }

    @Override
    public int getCount() {
        return mlist.size();
    }

    @Override
    public Object getItem(int position) {
        return mlist.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        OneClassifyHolder oneClassifyHolder;
        if (convertView == null) {
            oneClassifyHolder = new OneClassifyHolder();
            convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.one_classify, parent, false);
            oneClassifyHolder.name = (TextView) convertView.findViewById(R.id.one_classify_name);
            oneClassifyHolder.tag = convertView.findViewById(R.id.one_classify_tag);
            convertView.setTag(oneClassifyHolder);
        } else {
            oneClassifyHolder = (OneClassifyHolder) convertView.getTag();
        }
        LinearLayout.LayoutParams params_tag = new LinearLayout.LayoutParams((int)(w*4), (int)(h*25));
        params_tag.setMargins(0, (int)(h*9), 0, (int)(h*9));
        params_tag.gravity = Gravity.CENTER;
        oneClassifyHolder.tag.setLayoutParams(params_tag);

        LinearLayout.LayoutParams params_name = new LinearLayout.LayoutParams(
                ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        if (selectedPosition == position) {
            oneClassifyHolder.tag.setVisibility(View.VISIBLE);
            params_name.setMargins((int)(w*18), (int)(h*30), (int)(w*18), (int)(h*30));
            oneClassifyHolder.name.setTextColor(Color.parseColor("#FF5E32"));
            oneClassifyHolder.name.setTextSize(TypedValue.COMPLEX_UNIT_PX,parent.getContext().getResources().
                    getDimensionPixelSize(R.dimen.one_classify_name_select));
        } else {
            oneClassifyHolder.tag.setVisibility(View.GONE);
            params_name.setMargins((int)(w*20), (int)(h*32), (int)(w*20), (int)(h*32));
            oneClassifyHolder.name.setTextColor(Color.parseColor("#999999"));
            oneClassifyHolder.name.setTextSize(TypedValue.COMPLEX_UNIT_PX,parent.getContext().getResources().getDimensionPixelSize(R.dimen.one_classify_name));
        }
        params_name.gravity = Gravity.CENTER;
        oneClassifyHolder.name.setLayoutParams(params_name);
        OneClassifyData oneClassifyData = mlist.get(position);
        oneClassifyHolder.name.setText(oneClassifyData.getCatName());
        return convertView;
    }

    public void selectPosition (int position) {
        selectedPosition = position;
    }
}
